<?php
class Category_Model_Things extends Zend_Db_Table{
	protected $_name = 'things';
	protected $_primary = 'thing_id';

	public function getThingById($thingId)
	{
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/things/route/getThingById/id/".$thingId);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
		return $data;
	}

	public function getThingsByCategory($category)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restGet("/things/route/getThingsByCategory/cate_id/".$category);
		$return = json_decode(json_encode((array) simplexml_load_string($return->getBody())),1);
		
		for ($i=0;$i <count($return);$i++)
				$data[$i] = $return['row_'.$i];
				
		return $data;
	}
	
	public function updateThingsCategory($thing_id,$new_category)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restPut("/things/route/updateThing/id/".urlencode($thing_id)."/cate_id/".urlencode($new_category));
		
		$result = $this->getThingById($thing_id);
		Util::update_index($result[0], $result[0]['type'],"update");
		
		return $return->getBody();
	}
	
	public function deleteThingsInCategory($category_id)
	{// co dung
		$client = new Zend_Rest_Client(HTTP_SERVICE);
		$return = $client->restDelete("/things/route/deleteThingsInCategory/id/".urlencode($category_id));
		
		$result = $this->getThingsByCategory($category_id);
		foreach($result as $row)
			Util::update_index($row, $row['type'],"delete");
		
		return $return->getBody();
	}
	
}
?>